package com.jinzhi.jzweb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.utils.StringUtil;
import com.jinzhi.jzapi.domain.UserDO;
import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.domain.param.ReceiveOrderQuery;
import com.jinzhi.jzweb.domain.vo.userAunt.UserVO;
import com.jinzhi.jzweb.service.*;
import com.jinzhi.sys.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jinzhi.jzweb.dao.JazOrderTakingDao;
import com.jinzhi.common.base.CoreServiceImpl;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 
 * <pre>
 * 后台需求和面试邀约中间表
 * </pre>
 * <small> 2024-08-01 15:19:16 | Long</small>
 */
@Service
public class JazOrderTakingServiceImpl extends CoreServiceImpl<JazOrderTakingDao, JazOrderTakingDO> implements JazOrderTakingService {

    /**
     * 查询出最大的订单编号
     * @return
     */
    @Override
    public Long selJazOrderTakingMax() {
        return baseMapper.selJazOrderTakingMax();
    }
    @Autowired
    private JazOrderTakingDao jazOrderTakingDao;

    @Autowired
    private JazSysProjectService jazSysProjectService;

    @Autowired
    private JazUserOrderService jazUserOrderService;

    @Autowired
    private UserDao userDao;

    @Autowired
    private JazUserAuntService jazUserAuntService;

    @Autowired
    private JazUserDemandService jazUserDemandService;

    /**
     * 分页查询接单数据
     *
     * @param receiveOrderQuery
     * @return
     */
    @Override
    public Page<JazOrderTakingDO> getReceiverOrderList(ReceiveOrderQuery receiveOrderQuery) {
        Page<JazOrderTakingDO> page = new Page<>(receiveOrderQuery.getPageNumber(),receiveOrderQuery.getPageSize());
        // 分页查询数据
        List<JazOrderTakingDO> jazUserInterview =  jazOrderTakingDao.getReceiverOrderList(page,receiveOrderQuery);

        // 获取订单状态
        for (JazOrderTakingDO jazOrderTakingDO : jazUserInterview) {
            // 获取需求信息
            JazUserDemandDO demandDO = jazUserDemandService.selectById(jazOrderTakingDO.getDemandId());
            if (demandDO ==null){
                return page.setRecords(jazUserInterview);
            }
            JazUserOrderDO jazUserOrderDOS = jazUserOrderService.selectOne(new EntityWrapper<JazUserOrderDO>()
                    .eq("initiate_id",jazOrderTakingDO.getInviterId())
                    .eq("signing_id",jazOrderTakingDO.getInviteeId())
                    .eq("project_id",demandDO.getProjectId())
            );
            if (jazUserOrderDOS!=null){
                jazOrderTakingDO.setStatus(jazUserOrderDOS.getStatus());
                jazOrderTakingDO.setWorkTime(jazUserOrderDOS.getStartTime());
                jazOrderTakingDO.setEndTime(jazUserOrderDOS.getEndTime());
            }
        }
        // 过滤订单数据
        if (receiveOrderQuery.getStatus() != null) {
            jazUserInterview = jazUserInterview.stream()
                    .filter(order->order.getStatus()!=null)
                    .filter(order -> order.getStatus().equals(receiveOrderQuery.getStatus()))
                    .collect(Collectors.toList());
        }

        //查询项目类型
        List<String> projectIds = jazUserInterview.stream().filter(a -> Objects.nonNull(a.getProjectId()))
                .map(JazOrderTakingDO::getProjectId).distinct().collect(Collectors.toList());
        if(ObjectUtils.isEmpty(projectIds)){
            return page;
        }
        List<String> projectIds1 = new ArrayList<>();
        for (int i = 0; i < projectIds.size(); i++) {
            projectIds1.addAll(Arrays.asList(projectIds.get(i).split(",")));
        }
        List<JazSysProjectDO>  jazSysProjectDOS = jazSysProjectService.selectList(new EntityWrapper<JazSysProjectDO>().in("id",projectIds1));
        jazUserInterview.forEach(a-> {
            a.setProjectName(jazSysProjectDOS.stream().filter(b -> Objects.nonNull(a.getProjectId())).
                    filter(b -> a.getProjectId().contains(b.getId().toString())).
                    map(JazSysProjectDO::getName).collect(Collectors.joining(",")));
        });

        page.setRecords(jazUserInterview);
        return page;
    }


    /**
     * 根据id进行查询
     *
     * @param id
     * @return
     */
    @Override
    public JazOrderTakingDO getReceiverOrderById(Long id) {
        JazOrderTakingDO jazOrderTakingDO=jazOrderTakingDao.getReceiverOrderById(id);
        if (jazOrderTakingDO==null){
            return null;
        }

        // 查询需求信息
        JazUserDemandDO demandDO = jazUserDemandService.selectById(jazOrderTakingDO.getDemandId());
        if (demandDO==null){
            return null;
        }
        jazOrderTakingDO.setJazUserDemandDO(demandDO);

        // 查询订单状态
        JazUserOrderDO jazUserOrderDOS = jazUserOrderService.selectOne(new EntityWrapper<JazUserOrderDO>()
                .eq("initiate_id",jazOrderTakingDO.getInviterId())
                .eq("signing_id",jazOrderTakingDO.getInviteeId()));
        if (jazUserOrderDOS!=null){
            jazOrderTakingDO.setStatus(jazUserOrderDOS.getStatus());
            jazOrderTakingDO.setWorkTime(jazUserOrderDOS.getStartTime());
            jazOrderTakingDO.setEndTime(jazUserOrderDOS.getEndTime());
        }

        //查询阿姨信息
        UserDO userDO = userDao.selectById(jazOrderTakingDO.getInviteeId());
        if (userDO==null){
            return null;
        }
        List<UserVO> list = new ArrayList<>();
        // 循环赋值
        UserVO userVO = new UserVO();
        userVO.setSysUserid(userDO.getId());
        userVO.setName(userDO.getName());
        userVO.setMobile(userDO.getOrderPhone());
        userVO.setSex(userDO.getSex());
        userVO.setAge(userDO.getAge());
        userVO.setEducation(userDO.getEducation());
        // 查询月嫂信息
        JazUserAuntDO jazUserAuntDO = jazUserAuntService.selectOne(new EntityWrapper<JazUserAuntDO>().eq("sys_userid", userDO.getId()));
        if (jazUserAuntDO != null){
            userVO.setWorkExperience(jazUserAuntDO.getWorkExperience());
            userVO.setExperience(jazUserAuntDO.getExperience());
            userVO.setInchPhoto(jazUserAuntDO.getInchPhoto());
            // 查询月嫂类型
            if(StringUtil.notEmpty(jazUserAuntDO.getProjectId())){
                String[] projects = jazUserAuntDO.getProjectId().split(",");
                List<JazSysProjectDO>  jazSysProjectDOS = jazSysProjectService.selectList(new EntityWrapper<JazSysProjectDO>().in("id",projects));
                userVO.setProjectName(jazSysProjectDOS.stream().map(JazSysProjectDO::getName).collect(Collectors.joining(",")));
            }
        }
        list.add(userVO);
        jazOrderTakingDO.setUserList(list);

        //查询邀约人项目类型
        if(jazOrderTakingDO.getProjectId()!=null){
            JazSysProjectDO  jazSysProjectDOS = jazSysProjectService.selectById(jazOrderTakingDO.getProjectId());
            jazOrderTakingDO.setProjectName(jazSysProjectDOS.getName());
        }

        return jazOrderTakingDO;
    }
}
